package gui;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

/**
 * COPYRIGHT (C) 2009. Denny Tran, Omar Jaime, David Deng, Julie Hsieh. All Rights Reserved
 * Class is a ViewPanel containing JComponents asking for new member information.  
 * @author Denny Tran, Omar Jaime, David Deng, Julie Hsieh 
 * @version 1.01 2009-05-05
 */
public class AddMember extends ViewPanel
{
    private JTextField nameField;
    private JTextField pswdField;
    private EditGroup editGpPanel;

    /**
     * Construct ViewPanel containing JComponents asking for new member information
     * @param viewManager the container of this panel
     * @param identification specific identification used to reference this panel
     */
    public AddMember(final ViewManager viewManager, String identification)
    {
        super(viewManager, identification);
        
        editGpPanel = (EditGroup) viewManager.getView("edit group");
        
        Dimension labelSize = new Dimension(100, 20);
        int labelX = 60;
        int labelY = 40;
        int labelSpread = 30;
        
        // labels
        JLabel title = new JLabel("Add Member");
        title.setFont(new Font("Serif", Font.PLAIN, 24));
        title.setSize(150, 30);
        title.setLocation(60, 0);
        this.add(title);
        
        JLabel nameLabel = new JLabel("Name:");
        nameLabel.setSize(labelSize);
        nameLabel.setLocation(labelX,labelY);
        labelY += labelSpread;
        this.add(nameLabel);
    
        JLabel pswdLabel = new JLabel("Initial Password:");
        pswdLabel.setSize(labelSize);
        pswdLabel.setLocation(labelX, labelY);
        labelY += labelSpread;
        this.add(pswdLabel);
        
        // text fields
        Dimension textFieldSize = new Dimension(150,20);
        int textFieldX = labelX + (int)labelSize.getWidth() + 20;
        int textFieldY = 40;
        // use same spread as labels
        
        nameField = new JTextField();
        nameField.setSize(textFieldSize);
        nameField.setLocation(textFieldX, textFieldY);
        textFieldY += labelSpread;
        this.add(nameField);
        
        pswdField = new JTextField();
        pswdField.setSize(textFieldSize);
        pswdField.setLocation(textFieldX, textFieldY);
        textFieldY += labelSpread;
        this.add(pswdField);
        
        //buttons
        
        JButton confirmButton = new JButton("OK");
        confirmButton.setSize(80,20);
        confirmButton.setLocation(100, 300);
        confirmButton.addActionListener(new ActionListener()
        {
			public void actionPerformed(ActionEvent arg0)
			{
				String name = nameField.getText();
				if(name.isEmpty())
				{
					JOptionPane.showMessageDialog(null, "Insert Document Name");
					return;
				}
				else
				{
					editGpPanel.addMember(name);
					viewManager.showView("edit group");
				}
			}        	
        });
        this.add(confirmButton);
        
        JButton cancelButton = new JButton("Cancel");
        cancelButton.setSize(80,20);
        cancelButton.setLocation(200, 300);
        cancelButton.addActionListener(viewManager.addLink("edit group"));
        this.add(cancelButton);
    }

    /**
     * Clears all text fields and check boxes
     */
    public void clearView()
    {
        nameField.setText("");
        pswdField.setText("");
    }
}